<template>
  <el-dialog :visible="isShow" :title="{add:'新增权限',edit:'编辑权限'}[mode]" @close="closeEvent">
    <el-form label-width="120px" :model="form" :rules="rules" ref="form">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description"></el-input>
      </el-form-item>
      <el-form-item label="企业可见" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
          active-text="可见"
          inactive-text="不可见"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
        <div>
            <el-button @click="closeEvent">取消</el-button>
            <el-button type="primary" @click="submit">确定</el-button>
        </div>
    </template>
  </el-dialog>
</template>

<script>
import {sysPermissionPost,sysPermissionPut}from '@/api/permission'
export default {
  props: {
    isShow: {
      default: false,
      type: Boolean
    }
  },
  model: {
    prop: 'isShow'
  },
  data () {
    return {
        mode:'add',
      form: {
        name: '', //string	非必须
        code: '', //string	非必须
        description: '', //string	非必须
        enVisible: '0', //string	非必须
        type: '', //number	非必须
        pid: '', //string	非必须
        id: '' //string	必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }],
        enVisible: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods:{
submit(){
    this.$refs.form.validate(async result=>{
        if(result){
            if(this.mode==='add'){

                await sysPermissionPost(this.form)
            }else {
                await sysPermissionPut(this.form)
            }
            this.$message.success(this.mode==='add'?'新增成功':'编辑成功')
        //关闭弹窗
        this.closeEvent()
        //刷新父级列表
        this.$emit('getData')
            console.log(result);
        }
    })
},
closeEvent(){
    this.$refs.form.resetFields()
    this.form={
        name: '', //string	非必须
        code: '', //string	非必须
        description: '', //string	非必须
        enVisible: '0', //string	非必须
        type: '', //number	非必须 1代表菜单，2代表权限点
        pid: '', //string	非必须
        id: '' //string	必须
    }
    this.$emit('input',false)
}
  }
}
</script>

<style></style>
